﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;



using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;//引入命名空间

public partial class addimg : System.Web.UI.Page
{
   
  private static string location1;
     private static string location2;
    private static DataClassesDataContext da = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)//首次执行页面
        {
            SFUPC();
        }
    }
    //protected void Button1_Click(object sender, EventArgs e)
    //{
    //    string fileName = FileUpload1.FileName;
    //    string location = Server.MapPath("./ProductImages/") + fileName;
    //    location1 = "ProductImages/" + fileName;
    //    FileUpload1.SaveAs(location);
    //}
    //protected void Button2_Click(object sender, EventArgs e)
    //{
    //    string fileName = FileUpload2.FileName;
    // string   location = Server.MapPath("./ProductImages/") + fileName;
    //    location2 = "ProductImages/" + fileName;
    //    FileUpload2.SaveAs(location);
    //}
    //protected void Button3_Click(object sender, EventArgs e)
    //{
    //    string fileName = FileUpload1.FileName;
    //   // string location1 = Server.MapPath("./ProductImages/") + fileName;
    //    string fileName2 = FileUpload2.FileName;
    // //   string location2 = Server.MapPath("./ProductImages/") + fileName2;
    //    da.StoredProcedure2(location1, location2);
    //}
    #region  该方法用于将当前页面上传文件控件集保存到Session中
    private void SFUPC()
    {
        FileUpload myth1 = (FileUpload)this.FileUpload1;
       
        ArrayList AL = new ArrayList();//动态增加数组
        foreach (Control FUC in myth1.Parent.Controls)
        {

            //在表格中查找出FileUpload控件添加到ArrayList中
          
                    if (FUC.GetType().ToString() == "System.Web.UI.WebControls.FileUpload")
                    {
                        FileUpload FU = (FileUpload)FUC;
                        //设置FileUpload的样式
                       
                        //添加FileUpload控件
                        AL.Add(FU);
                
                }
            }
        
        //把ArrayList添加到Session中
        Session.Add("FilesControls", AL);
    }
    #endregion

    #region 该方法用于添加一个上传文件的控件
    private void InsertC()
    {
        //实例化ArrayList
        ArrayList AL = new ArrayList();
  //      this.Tab_UpDownFile.Rows.Clear(); //清除id为F表格里的所有行
   //     GetInfo();
        //表示 HtmlTable 控件中的 <tr> HTML 元素
      //  HtmlTableRow HTR = new HtmlTableRow();
        //表示 HtmlTableRow 对象中的 <td> 和 <th> HTML 元素
      //  HtmlTableCell HTC = new HtmlTableCell();
        FileUpload fla =new FileUpload();
        fla.Style["float"] = "left";
        //foreach (Control C in Tab_UpDownFile.Controls)
        //{
        //    //在表格中查找出FileUpload控件添加到ArrayList中
        //    if (C.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTableRow")
        //    {
        //        HtmlTableCell HTC = (HtmlTableCell)C.Controls[0];
        //        FileUpload myth1 = (FileUpload)HTC.FindControl("FileUpload1");
        //      //  myth1.Controls.Add(fla);
        //        myth1.Parent.Controls.Add(fla);
        //    }
        //}
        FileUpload myth1 = (FileUpload)this.FileUpload1;
        myth1.Parent.Controls.Add(fla);
        //在单元格中添加一个FileUpload控件
        //HTC.Controls.Add(fla);
        ////在行中添加单元格
        //HTR.Controls.Add(HTC);
        ////在表中添加行
        //Tab_UpDownFile.Rows.Add(HTR);
        SFUPC();
    }
    #endregion

    #region 该方法用于将保存在Session中的上传文件控件集添加到表格中
    private void GetInfo()
    {
        ArrayList AL = new ArrayList();
        if (Session["FilesControls"] != null)
        {
            AL = (ArrayList)Session["FilesControls"];
            for (int i = 1; i < AL.Count; i++)
            {
                FileUpload myth1 = (FileUpload)this.FileUpload1;
                myth1.Parent.Controls.Add((System.Web.UI.WebControls.FileUpload)AL[i]);
               // HTC.Controls.Add((System.Web.UI.WebControls.FileUpload)AL[i]);
               
            }
        }
    }
    #endregion

    #region 该方法用于执行文件上传操作
    private void UpFile(ref string loalocation1, ref string loalocation2)
    {

        //获取文件夹路径
       // string FilePath = Server.MapPath("./") + "File";

        // 获取客户端上载文件的集合
        HttpFileCollection HFC = Request.Files;
        int i = 0;
        while (i < HFC.Count)
        {
            //访问指定的文件
            HttpPostedFile UserHPF1 = HFC[i];
            HttpPostedFile UserHPF2 = HFC[i+1];
            try
            {
                //判断文件是否为空
                if (UserHPF1.ContentLength > 0 && UserHPF2.ContentLength > 0)
                {
                    //将上传的文件存储在指定目录下
                    UserHPF1.SaveAs(Server.MapPath("./ProductImages/") + System.IO.Path.GetFileName(UserHPF1.FileName));
                    UserHPF2.SaveAs(Server.MapPath("./ProductImages/") + System.IO.Path.GetFileName(UserHPF2.FileName));
                    location1 = "ProductImages/" + UserHPF1.FileName;
                    location2 = "ProductImages/" + UserHPF2.FileName;
                    da.StoredProcedure2(location1, location2);
                }
            }
            catch
            {
                LblMessage.Text = "上传失败！";
            }
            i += 2;
        }
        if (Session["FilesControls"] != null)
        {
            Session.Remove("FilesControls");
        }
        LblMessage.Text = "上传成功！";

    }
    #endregion
    #region 调用InsertC方法，实现添加FileUpLoad控件的功能
    protected void BtnAdd_Click(object sender, EventArgs e)
    {
         FileUpload myth1 = (FileUpload)this.FileUpload1;
         foreach (Control FUC2 in myth1.Parent.Controls)
         {
             if (FUC2 != (FileUpload)this.FileUpload1)
             {
                 FUC2.Controls.Clear();
             }
         }
      //  this.FileUpload1.Parent.Controls.Clear();
       // InsertC();//执行添加控件方法
        GetInfo();
        FileUpload fla = new FileUpload();
      
        fla.Style["float"] = "left";
       
        myth1.Parent.Controls.Add(fla);
        SFUPC();
        LblMessage.Text = "";
    }
    #endregion
    #region 实现文件上传的功能
    protected void BtnUpFile_Click(object sender, EventArgs e)
    {
        if (this.FileUpload1.PostedFile.FileName != "")
        {
            UpFile(ref location1,ref location2);//执行上传文件
            SFUPC();
        }
        else
        {
            LblMessage.Text = "对不起，上传文件为空，请选择上传文件！";
        }

    }
    #endregion
}